// api/g/sun_piece?type=props
// export async function loadPiece(
//   type: string | 'props' | 'piece' | 'component'
// ): Promise<object> {
//   try {
//     const response = await fetch(`/api/g/sun_piece?type=${type}`)

import { Piece, PieceComponentPack, PieceProps } from '@/types/piece'
import { ROUTE_APP } from '@/lib/consts'
//     if (!response.ok) {
//       throw new Error(`HTTP error! status: ${response.status}`)
//     }

//     const data = await response.json()
//     return data
//   } catch (error) {
//     console.error('Error loading piece data:', error)
//     return {}
//   }
// }

export async function loadPiece(query: object): Promise<Piece[]> {
  try {
    const response = await fetch(
      `${ROUTE_APP}/sun_piece?query=${JSON.stringify(query)}`
    )

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }

    const data = await response.json()
    return data.data.items || []
  } catch (error) {
    console.error('Error loading piece data:', error)
    return []
  }
}

// 创建碎片
export async function createPiece(data: Piece): Promise<Piece | null> {
  try {
    const response = await fetch(`${ROUTE_APP}/sun_piece`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }

    const result = await response.json()
    return result.data || null
  } catch (error) {
    console.error('Error creating piece:', error)
    return null
  }
}

// 创建属性
export async function createPieceProps(
  data: PieceProps
): Promise<PieceProps | null> {
  try {
    const response = await fetch(`${ROUTE_APP}/sun_piece_props`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }

    const result = await response.json()
    return result.data || null
  } catch (error) {
    console.error('Error creating piece props:', error)
    return null
  }
}

// t_sun_component_pack
export async function loadPackage(query: object): Promise<object> {
  try {
    const response = await fetch(
      `/api/g/sun_component_pack?query=${JSON.stringify(query)}`
    )

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }

    const data = await response.json()
    return data
  } catch (error) {
    console.error('Error loading package data:', error)
    return {}
  }
}

export async function loadPieceProps(): Promise<PieceProps[]> {
  try {
    // size:999
    const params = { size: 999 }
    const response = await fetch(
      `${ROUTE_APP}/sun_piece_props?query` + JSON.stringify(params)
    )

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }

    const data = await response.json()
    return data.data.items || []
  } catch (error) {
    console.error('Error loading piece props:', error)
    return []
  }
}

// 指定id
export async function loadPieceComponentPackById(
  id: number
): Promise<PieceComponentPack | null> {
  try {
    const response = await fetch(`${ROUTE_APP}/sun_component_pack/${id}`)

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }

    const data = await response.json()
    return data.data || null
  } catch (error) {
    console.error('Error loading piece by id:', error)
    return null
  }
}

export async function loadPieceComponentPack(): Promise<PieceComponentPack[]> {
  try {
    // size:999
    const params = { size: 999, desc: ['updated_at'] }
    const response = await fetch(
      `${ROUTE_APP}/sun_component_pack?query=` + JSON.stringify(params)
    )

    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }

    const data = await response.json()
    return data.data.items || []
  } catch (error) {
    console.error('Error loading piece props:', error)
    return []
  }
}

// 保存PieceComponentPack
export async function savePieceComponentPack(
  data: PieceComponentPack
): Promise<PieceComponentPack | null> {
  try {
    // created_by字段要设置为0
    const response = await fetch(`${ROUTE_APP}/sun_component_pack`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(data)
    })
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`)
    }
    const result = await response.json()
    return result.data
  } catch (error) {
    console.error('Error saving piece component pack:', error)
    return null
  }
}
